package Tools;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import Windows.PicPanel;
import Windows.Search.SearchTableModel.TreeTableValue;
import Windows.Search.SearchTableModel.toolColumn;
import XML.XMLNode;

public class TableStyle {
	private static PicPanel mp = new PicPanel("Skin/resault/default.png");

	/**
	 * 设置表格行样式
	 * 
	 * @param table
	 */
	public static void makeFace(JTable table) {

		try {

			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				@Override
				public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
					Color foreground = Color.BLACK;
					Color background = Color.WHITE;
					Color SelectionBackground = table.getSelectionBackground();// 选中背景
					Color SelectionForeground = table.getSelectionForeground();// 选中前景
					if (value.getClass() == TreeTableValue.class) {

						TreeTableValue ttv = (TreeTableValue) value;
						value = ttv.getValue();// 行的显示对象
						value = value == null ? "" : value;

						// ==============设置行底色开始===============
						if (ttv.getXf().isLeafage()) {

							background = new Color(120, 120, 120); // 设置叶行底色 灰色

						} else {

							// this.setBorder(new LineBorder(Color.BLUE,1));

							ArrayList<XMLNode> al = ttv.getStm().getCoequality(ttv.getXf());// 取同一级的所有节点
							int i = al.indexOf(ttv.getXf());
							int k = ttv.getStm().getLayer(ttv.getXf()) - 1;
							int se = 5;// 节点深度色差
							if (i % 2 == 0)
								background = new Color(255 - k * se, 255 - k * se, 255 - k * se); // 设置偶数行底色
																									// 蓝色
							else
								background = new Color(210 - k * se, 220 - k * se, 215 - k * se); // 设置奇数行底色
							// 红色
						}
						// ==============设置行底色结束===============
						// System.out.println("!"+value);
						if (value != null) {
							if (value.getClass() == toolColumn.class) {
								// System.out.println("安全列渲染"+background);
								toolColumn jp = (toolColumn) value;
								jp.UpdateICO();
								jp.getJl().setForeground(isSelected ? SelectionForeground : table.getForeground());
								jp.setBackground(isSelected ? SelectionBackground : background);
								return jp;

							} else if (value.getClass() == PicPanel.class) {
								PicPanel pp = (PicPanel) value;
								pp.setBackground(isSelected ? SelectionBackground : background);
								return pp;
							}
						}
					} else {// =======其他Value的风格=========
						if (row % 2 == 0)
							background = Color.WHITE; // 设置奇数行底色 蓝色
						else
							background = new Color(210, 220, 215); // 设置偶数行底色
						// 红色
					}
					value = value == null ? "" : value;

					setBackground(background);

					return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				}
			};

//			DefaultTableCellRenderer tol = new DefaultTableCellRenderer() {
//				public Component getTableCellRendererComponent(JTable table,
//						Object value, boolean isSelected, boolean hasFocus,
//						int row, int column) {
//					if (value.getClass() == TreeTableValue.class) {
//						TreeTableValue ttv = (TreeTableValue) value;
//						if (column == 0 || column == 6)
//							System.out.println(ttv.getXf());
//						value = ttv.getValue();
//						return ttv.getXf().isLeafage() ? mp
//								: (value.getClass() == toolColumn.class ? (toolColumn) value
//										: mp); // ttv.getValue();
//					} else {
//						System.out.println("未知渲染器：TableStyle  >> "
//								+ value.getClass());
//					}
//					return super.getTableCellRendererComponent(table, value,
//							isSelected, hasFocus, row, column);
//				}
//			};

//			DefaultCellEditor tce = new DefaultCellEditor(new JCheckBox()) {
//
//				public Component getTableCellEditorComponent(JTable table,
//						Object value, boolean isSelected, int row, int column) {
//					System.out.println("列编辑渲染器返回！" + value.getClass());
//					if (value.getClass() == TreeTableValue.class) {
//						TreeTableValue ttv = (TreeTableValue) value;
//						return (ToolColumnPanel) ttv.getValue();
//					}
//					return null;
//				}
//			};

			// 列模型调整

			for (int i = 0; i < table.getColumnCount(); i++) {
				Class<?> clas = table.getModel().getColumnClass(i);
				if (clas == TreeTableValue.class || clas == String.class)
					table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);// 所有列的用同一个渲染器展示
				// else if (table.getModel().getColumnClass(i) ==
				// ImageIcon.class) {
				// table.getColumn(table.getColumnName(i))
				// .setCellRenderer(tol);
				// } else if (table.getModel().getColumnClass(i) ==
				// toolColumn.class) {
				// table.getColumn(table.getColumnName(i))
				// .setCellRenderer(tol);
				// // table.setCellEditor(tce);
				// }
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	/**
	 * 表格风格设置
	 * 
	 * @param table
	 */
	public static void setDefaultStyle(JTable table) {
		table.setShowVerticalLines(false);
		table.setShowHorizontalLines(false);
		// table.setRowMargin(2);
		table.setIntercellSpacing(new Dimension(0, 0));
		table.setShowGrid(false);
		table.setFocusable(false);
		table.setRowHeight(20);
		table.getColumnModel().setColumnMargin(0);
		table.setSelectionBackground(new Color(43, 73, 240));
		table.setSelectionForeground(Color.WHITE);
		makeFace(table);

	}

	public static void setStyle(JTable jt, int index) {
		setDefaultStyle(jt);
	}
}
